package cn.sumpu.app.wlcity.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Common {
	public static final int PAGE_SIZE_VOTE = 10;
	public static final int PAGE_SIZE_VOTE_ITEM = 6;
	public static final int PAGE_SIZE_VOTE_FRONT = 10;
	public static final int PAGE_SIZE_SECKILL = 5;
	public static final int PAGE_SIZE_SECKILLFIVE = 100;
	public static final int PAGE_SIZE_CONTENT = 15;
	
	public static final int PAGE_SIZE_HOT = 100;
	public static final int PAGE_SIZE_INTRODUCTION = 100;
	public static final int PAGE_SIZE_TOGETHER = 100;
	public static final int PAGE_SIZE_COMPANY = 100;
	/**
	 * 温泉季应用
	 */
	public static final String APP_SPRING = "01";
	/**
	 * 灯会应用
	 */
	public static final String APP_LIGHT = "02";
	
	/**
	 * 投票应用
	 */
	public static final String APP_VOTE = "03";
	/**
	 * 最新看点
	 */
	public static final String TYPE_HOT = "01";
	/**
	 * 温泉简介
	 */
	public static final String TYPE_INTRODUCTION = "02";
	
	/**
	 * 合作商家优惠
	 */
	public static final String TYPE_TOGETHER = "03";
	
	/**
	 * 合作商家展示
	 */
	public static final String TYPE_COMPANY = "04";
	
	/**
	 * 灯会扫描
	 */
	public static final String LIGHT_SCAN = "05";
	/**
	 * 服务指南
	 */
	public static final String LIGHT_GUIDE = "06";
	
	/**
	 * 活动介绍 app:01;
	 */
	public static final String VOTE_CONTENT_TYPE_01 = "01";
	/**
	 * 最新消息 app:01;
	 */
	public static final String VOTE_CONTENT_TYPE_02 = "02";
	/**
	 * 抽奖规则 奖品展示 app:01;
	 */
	public static final String VOTE_CONTENT_TYPE_03 = "03";
	
	public static String getFormatDate(Date date) {
		return getFormatDate(date, "yyyyMMdd");
	}
	
	public static String getFormatDate(Date date, String format) {
		DateFormat f = new SimpleDateFormat(format);
		return f.format(date);
	}
	
	public static String cutStr(String str, int length, String fix) {
		if (empty(str))
			return str;
		if (length > str.length())
			return str;
		return str.substring(0, length) + fix;
	}
	
	@SuppressWarnings("unchecked")
	public static boolean empty(Object obj) {
		if (obj == null) {
			return true;
		} else if (obj instanceof String && (obj.equals("") || obj.equals("0"))) {
			return true;
		} else if (obj instanceof Number && ((Number) obj).doubleValue() == 0) {
			return true;
		} else if (obj instanceof Boolean && !((Boolean) obj)) {
			return true;
		} else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
			return true;
		} else if (obj instanceof Map && ((Map) obj).isEmpty()) {
			return true;
		} else if (obj instanceof Object[] && ((Object[]) obj).length == 0) {
			return true;
		}
		return false;
	}
	
	public static Date addMinutes(Date date, int minutes) {
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minutes);
		return date = cal.getTime();
	}
	
	public static String nl2br(String text) {
		if (text == null || text.length() == 0) {
			return text;
		}
		StringBuffer sb = new StringBuffer(text.length() * 2);
		StringCharacterIterator iterator = new StringCharacterIterator(text);
		char character = iterator.current();
		while (character != StringCharacterIterator.DONE) {
			switch (character) {
			case '\r':
				sb.append("<br/>");
				sb.append(character);
				character = iterator.next();
				if (character == '\n') {
					character = iterator.next();
				}
				break;
			case '\n':
				sb.append("<br/>");
				sb.append(character);
				character = iterator.next();
				if (character == '\r') {
					sb.append(character);
					character = iterator.next();
				}
				break;
			default:
				sb.append(character);
				character = iterator.next();
				break;
			}
		}
		return sb.toString();
	}
	/**  
     * @author grieous
     * 基本功能：过滤所有以"<"开头以">"结尾的标签
     * @param str  
     * @return String  
     */  
    public static String filterHtml(String str) {   
        Pattern pattern = Pattern.compile("<([^>]*)>");   
        Matcher matcher = pattern.matcher(str);   
        StringBuffer sb = new StringBuffer();   
        boolean result1 = matcher.find();   
        while (result1) {   
            matcher.appendReplacement(sb, "");   
            result1 = matcher.find();   
        }   
        matcher.appendTail(sb);   
        return sb.toString();   
    }
}
